﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using IDML.BusinessObject;
using IDML.BusinessLogic;


namespace IDML.Web.Modules.Patient
{
    public partial class View : System.Web.UI.Page
    {
        PatientBO _patientBo;
        PaymentHistoryBO _paymentHistoryBo;
        PatientBL _patientBl;

        DataTable dtPayDetails;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PATID"] != null)
            {
                int patID = Convert.ToInt32(Request.QueryString["PATID"]);
                LoadPatientDetails(patID);
                LoadPatientTestDetails(patID);
            }
        }

        private void LoadPatientDetails(int patID)
        {
            _patientBo = new PatientBO();
            _patientBl = new PatientBL();
            _patientBo.PatientId = patID;

            List<PatientBO> patientList = _patientBl.Patient_LoadDetails(patID);

            lblFirstName.Text = patientList[0].FirstName;
            lblLastName.Text = patientList[0].LastName;
            lblPhoneNumber.Text = patientList[0].PhoneNumber;
            lblAge.Text = patientList[0].Age.ToString();
        }

        protected void btnAddTestPay_Click(object sender, EventArgs e)
        {
            TestBO _testBO = new TestBO();
            TestBL _testBL = new TestBL();

            dtPayDetails = new DataTable();

            _testBO.TestCode = txtTestCode.Text;
            List<TestFeeStructDetailsBO> testFeeStructDet = _testBL.GetTestFeeStructure(_testBO);


            if (ViewState["PayDetails"] == null)
            {
                dtPayDetails.Columns.Add("TestId", typeof(int));
                dtPayDetails.Columns.Add("TestFeeStructureId", typeof(int));
                dtPayDetails.Columns.Add("TestCode", typeof(string));
                dtPayDetails.Columns.Add("TestName", typeof(string));
                dtPayDetails.Columns.Add("Amount", typeof(decimal));
            }
            else
            {
                dtPayDetails = (DataTable)ViewState["PayDetails"];
            }

            dtPayDetails.Rows.Add(testFeeStructDet[0].TestId, testFeeStructDet[0].TestFeeStructureId, testFeeStructDet[0].TestCode, testFeeStructDet[0].TestName, testFeeStructDet[0].Amount);
            ViewState["PayDetails"] = dtPayDetails;
                       
            gwPaymentView.DataSource = ViewState["PayDetails"];
            gwPaymentView.DataBind();

            ViewState["TotalAmt"] = Convert.ToInt32(ViewState["TotalAmt"]) + testFeeStructDet[0].Amount;
            lblTotAmt.Text = ViewState["TotalAmt"].ToString();
        }

        protected void gwPaymentView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int testID = Convert.ToInt32(gwPaymentView.DataKeys[e.RowIndex].Values["TestId"]);
            DeleteAccompanyingData(testID);
            dtPayDetails = (DataTable)ViewState["PayDetails"];

            lblTotAmt.Text = ViewState["TotalAmt"].ToString();

            gwPaymentView.DataSource = dtPayDetails;
            gwPaymentView.DataBind();
        }

        private void DeleteAccompanyingData(int testID)
        {
            dtPayDetails = new DataTable();
            dtPayDetails = (DataTable)ViewState["PayDetails"];

            for (int i = dtPayDetails.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dtPayDetails.Rows[i];
                if (Convert.ToInt32(dr["TestId"]) == testID)
                {
                    ViewState["TotalAmt"] = Convert.ToInt32(ViewState["TotalAmt"]) - Convert.ToInt32(dr["Amount"]);
                    dr.Delete();
                }
            }

            ViewState["PayDetails"] = dtPayDetails;
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            IDMLSession idmlSession = new IDMLSession();
            _patientBo = new PatientBO();
            _patientBl = new PatientBL();
            _paymentHistoryBo = new PaymentHistoryBO();
            TestFeeStructDetailsBO _testFeeStructDetailsBO;

            //Preparing to insert data to the "PaymentHistory" table
            _paymentHistoryBo.PatientId = Convert.ToInt32(Request.QueryString["PATID"]);
            _paymentHistoryBo.Amount = Convert.ToInt32(ViewState["TotalAmt"]);
            _paymentHistoryBo.PaidAmount = Convert.ToDecimal(txtAmtPaid.Text);
            _paymentHistoryBo.IUserID = _paymentHistoryBo.UUserID = Convert.ToInt32(idmlSession.User.UserID);


            //Preparing to insert data to the "PaymentHistoryDetails" table
            DataTable DT = (DataTable)ViewState["PayDetails"];
           
            List<TestFeeStructDetailsBO> _testFeestructDet = new List<TestFeeStructDetailsBO>();
            
            foreach (DataRow row in DT.Rows)
            {
                _testFeeStructDetailsBO = new TestFeeStructDetailsBO();
                _testFeeStructDetailsBO.TestId = Convert.ToInt32(row[0]);
                _testFeeStructDetailsBO.TestFeeStructureId = Convert.ToInt32(row[1]);
                _testFeeStructDetailsBO.TestCode = row[2].ToString();
                _testFeeStructDetailsBO.TestName = row[3].ToString();
                _testFeeStructDetailsBO.Amount = Convert.ToDecimal(row[4]);

                _testFeestructDet.Add(_testFeeStructDetailsBO);
            }

            _patientBl.Patient_AddTestAndPaymentDetails(_paymentHistoryBo, _testFeestructDet);
        }

        private void LoadPatientTestDetails(int patID)
        {
            _patientBo = new PatientBO();
            _patientBl = new PatientBL();
            _patientBo.PatientId = patID;

            gwPatientTest.DataSource = null;
            gwPatientTest.DataSource = _patientBl.Patient_LoadTestDetails(_patientBo);
            gwPatientTest.DataBind();
        }
    }
}